package com.example.sqlite.sqlit_Litepal;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.sqlite.R;

import org.litepal.LitePal;

import java.util.List;

public class Activity_LitePal extends AppCompatActivity implements View.OnClickListener {
    private EditText litePal_id;
    private EditText litePal_age;
    private EditText litePal_name;
    private EditText litePal_height;
    private EditText litePal_weight;
    private TextView litePal_textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lite_pal);
        LitePal.getDatabase();

        initView();
    }

    private void initView() {
        Button btn_db_LitePal_query = findViewById(R.id.btn_db_LitePal_query);
        Button btn_db_LitePal_insert = findViewById(R.id.btn_db_LitePal_insert);
        Button btn_db_LitePal_update = findViewById(R.id.btn_db_LitePal_update);
        Button btn_db_LitePal_delete = findViewById(R.id.btn_db_LitePal_delete);
        Button bt_LitePal_clear = findViewById(R.id.bt_LitePal_clear);
        litePal_id = findViewById(R.id.litePal_id);
        litePal_age = findViewById(R.id.litePal_age);
        litePal_name = findViewById(R.id.litePal_name);
        litePal_height = findViewById(R.id.litePal_height);
        litePal_weight = findViewById(R.id.litePal_weight);
        litePal_textView = findViewById(R.id.litePal_textView);

        btn_db_LitePal_query.setOnClickListener(this);
        btn_db_LitePal_insert.setOnClickListener(this);
        btn_db_LitePal_update.setOnClickListener(this);
        btn_db_LitePal_delete.setOnClickListener(this);
        bt_LitePal_clear.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String idS = litePal_id.getText().toString();
        String ageS = litePal_age.getText().toString();
        String nameS = litePal_name.getText().toString();
        String heightS = litePal_height.getText().toString();
        String weightS = litePal_weight.getText().toString();
        People people = new People();
        switch (v.getId()) {
            case R.id.btn_db_LitePal_query:
                List<People> peopleList = LitePal.
                        select( "people_age", "people_name","people_height","people_weight")  //限定要查询的列以后，对于find()所返回列表中所有的对象，没有查询的数据域表现为默认值
                        .where("people_name = ?", nameS)//修饰条件
//                        .order("pages")//设置排序方式
//                        .limit(10)//限制查询返回的行数
//                        .offset(10)//设置偏移量
                        .find(People.class);
                StringBuilder tvData = new StringBuilder();
                for (People people1 : peopleList) {
                    tvData.append(people1.toString());
                }
                litePal_textView.setText(tvData.toString());
                break;
            case R.id.btn_db_LitePal_insert:
                people.setPeople_name(nameS);
                people.setPeople_age(Integer.valueOf(ageS));
                people.setPeople_weight(Double.valueOf(weightS));
                people.setPeople_height(Double.valueOf(heightS));
                people.save();
                break;
            case R.id.btn_db_LitePal_update:
                people.setPeople_name(nameS);
                people.setPeople_age(Integer.valueOf(ageS));
                people.setPeople_weight(Double.valueOf(weightS));
                people.setPeople_height(Double.valueOf(heightS));
                //people.update(idS);//按主键更新
                people.updateAll("people_name = ? ", nameS );//条件更新

                break;
            case R.id.btn_db_LitePal_delete:
                LitePal.deleteAll(People.class, "people_name =? ", nameS);
                break;
            case R.id.bt_LitePal_clear:
                litePal_age.setText("");
                litePal_name.setText("");
                litePal_height.setText("");
                litePal_weight.setText("");
                break;
        }
    }
}